﻿#include "pcrState.h"


QVector<QString> PCRState::CHANNELLIST = {"FAM", "VIC", "ROX", "CY5", ""};

PCRState::PCRState()
{

    // 初始化颜色信息
    colorChannel[0] = QColor(22, 119, 255);
    colorChannel[1] = QColor(103, 194, 58);
    colorChannel[2] = QColor(235, 137, 20);
    colorChannel[3] = QColor(245, 108, 105);

}

PCRState::~PCRState(){

    // 释放计算模型
    for(auto iter = modelList.begin(); iter != modelList.end(); iter ++){
        if(iter.value() != nullptr){
            delete iter.value();
        }
    }
}


QString getPosiName(int posi){

    QStringList letters = {"A", "B", "C", "D", "E", "F", "G", "H"};
    if(posi <= 0 || posi > 96){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]GetPosiName - invalid posiIdx - " + QString::number(posi));
        return "";
    }else{
        return letters[(posi-1)/12] + QString::number((posi-1)%12+1);
    }
}

int getPosiIdx(QString name){       // 范围: 1~96

    if(name.size() < 2){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]GetPosiIdx - invalid posiName - " + name);
        return 0;
    }
    int res = (name[0].toLatin1()-'A')*12 + name.mid(1).toInt();
    if(res <= 0 || res > 96){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]GetPosiIdx - invalid posiName - " + name);
        return 0;
    }
    return res;
}

int getChannelIdx(QString name){
    for(int i = 0; i < 4; i ++){
        if(PCRState::CHANNELLIST[i] == name){
            return i;
        }
    }
    LogHelperJ::write("ERROR: [Class]PCRState [Func]GetChannelIdx - invalid posiIdx - " + name);
    return 4;
}

bool PCRState::checkCurve(CURVE curve){
    if(!curveList.contains(curve)){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]CheckCurve - Curve NAN - " + QString::number(curve));
        return false;
    }if(!modelList.contains(curve)){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]CheckCurve - Model NAN - " + QString::number(curve));
        return false;
    }

    POSI posi = curve % 100;
    CHANNEL channel = curve / 100;
    if(!posiList.contains(posi)){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]CheckCurve - Posi NAN - " + QString::number(curve));
        return false;
    }if(!channelList.contains(channel)){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]CheckCurve - Channel NAN - " + QString::number(channel));
        return false;
    }if(!samplePannel.contains(posi)){
        LogHelperJ::write("ERROR: [Class]PCRState [Func]CheckCurve - Sample NAN - " + QString::number(curve));
    }
    return true;
}

//标准曲线编号: 99 199 299 399
QMap<int, QVector<QPointF>> PCRState::getStandardCurve(){

    QMap<int, QVector<QPointF>> res;

    if(!canAnalysis){ return res; }

    foreach(CHANNEL channel, standardLine.keys()){
        QVector<QPointF> line;
        double k = standardLine[channel][0];
        double b = standardLine[channel][1];
        line.push_back(QPointF(-5, -5*k+b));
        line.push_back(QPointF(5, 5*k+b));
        res[channel] = line;
    }

    return res;

}

